package com.dc.designPattern._15_iterator.impl;

import java.util.Iterator;

/**
 * Description: 迭代器模式_具体迭代器类_书架迭代器(管理迭代)
 * @author: gdc
 * @date: 2021/5/18
 * @version 1.0
 */
public class BookShelfIterator implements Iterator {

    /**
     * 书架类
     */
    private BookShelf bookShelf;
    /**
     * 当前索引位置
     */
    private int index = 0;

    public BookShelfIterator(BookShelf bookShelf) {
        this.bookShelf = bookShelf;
    }

    @Override
    public boolean hasNext() {
        if (index < bookShelf.getLength()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Object next() {
        Book book = bookShelf.getBook(index);
        // 获取数据位置后 +1
        index++;
        return book;
    }
}